package com.feng.test;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import android.widget.TextView;

public class TcpTestActivity extends Activity {
	TextView textlog;// Log for outputs

	Button buttonConnect;// (dis)connect Button
	Button button[] = new Button[9];
	SeekBar seekBar_arr[] = new SeekBar[8];// Seekbar to control the Servo
	//TextView seekBarValue[] = new TextView[8] ;// Textfield displaing the Value of the seekbar
	RotaryKnobView jogView;
	Boolean connected = false;// stores the connectionstatus
	TextView jogValue[] = new TextView[9];
	DataOutputStream dataOutputStream = null;// outputstream to send commands
	Socket socket = null;// the socket for the connection
	
	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// connect the view and the objects
		buttonConnect = (Button) findViewById(R.id.connect);
		textlog = (TextView) findViewById(R.id.textlog);
		//
		/*
		seekBar_arr[0] = (SeekBar) findViewById(R.id.seekBar1);
		seekBar_arr[1] =  (SeekBar) findViewById(R.id.seekBar2);
		seekBar_arr[2] =  (SeekBar) findViewById(R.id.seekBar3);
		seekBar_arr[3] =  (SeekBar) findViewById(R.id.seekBar4);
		seekBar_arr[4] =  (SeekBar) findViewById(R.id.seekBar5);
		seekBar_arr[5] =  (SeekBar) findViewById(R.id.seekBar6);
		seekBar_arr[6] =  (SeekBar) findViewById(R.id.seekBar7);
		seekBar_arr[7] =  (SeekBar) findViewById(R.id.seekBar8);
		
		
		
		
		//
		seekBarValue[0] = (TextView) findViewById(R.id.textView1);
		seekBarValue[1] = (TextView) findViewById(R.id.textView2);
		seekBarValue[2] = (TextView) findViewById(R.id.textView3);
		seekBarValue[3] = (TextView) findViewById(R.id.textView4);		
		seekBarValue[4] = (TextView) findViewById(R.id.textView5);
		seekBarValue[5] = (TextView) findViewById(R.id.textView6);
		seekBarValue[6] = (TextView) findViewById(R.id.textView7);
		seekBarValue[7] = (TextView) findViewById(R.id.textView8);
		
		
		//
		
		button[0] =  (Button) findViewById(R.id.button1);
		button[1] =  (Button) findViewById(R.id.button2);
		button[2] =  (Button) findViewById(R.id.button3);
		button[3] =  (Button) findViewById(R.id.button4);
		button[4] =  (Button) findViewById(R.id.button5);
		button[5] =  (Button) findViewById(R.id.button6);
		button[6] =  (Button) findViewById(R.id.button7);
		button[7] =  (Button) findViewById(R.id.button8);
		button[8] =  (Button) findViewById(R.id.button9);
		*/
		

		textlog.setText("Starting Client");// log that the App launched
		changeConnectionStatus(false);// change connectionstatus to
										// "disconnected"
	
		// ---------------Eventlisteners
		
		
		//button
		buttonConnect.setOnClickListener(buttonConnectOnClickListener);
		
		
		//
		/*
		for(int i = 0;i<8; i++){
			seekBar_arr[i].setMax(255);
			//add behavoir to seekbar change
		seekBar_arr[i].setOnSeekBarChangeListener(new SeekBarListenerBehavior(i));  
	
		}
		for(int i = 0;i<9; i++){
		//button
		button[i].setOnClickListener(new ButtonClickListenerBehavior(i));
		
		}
		*/
		
		
		
		jogView = (RotaryKnobView)findViewById(R.id.RotaryKnobView0);
		  jogView.setKnobListener(new RotaryKnobView.RotaryKnobListener()
		  {
		     @Override
		     public void onKnobChanged(int arg) {
		    	
		    	 get();
		      if (arg > 0){
		    	  //System.out.println(arg);
		      }
		         // rotate right 
		      else{
		    	  //System.out.println(arg);
		      }
		    	  
		         // rotate left 
		     }
		   });
		    
		  jogValue[0] = (TextView) findViewById(R.id.textView0);
		  
		  
	}
	
	void get(){
		System.out.println(jogView.getAngle());
		jogValue[0].setText(String.valueOf((int)jogView.getAngle()));					
		
		
	}
	// Method changes the connection status
	public void changeConnectionStatus(Boolean isConnected) {
		connected = isConnected;// change variable
		//seekBar_arr[0].setEnabled(isConnected);// enable/disable seekbar
		if (isConnected) {// if connection established
			buttonConnect.setText("disconnect");// change Buttontext
		} else {
			buttonConnect.setText("connect");// change Buttontext
		}
	}

	// Method appends text to the textfield and adds a newline character
	public void outputText(String msg) {
		textlog.append(msg + "\n");
	}
	
	/**
	 * listener for 9 button*/
	public class ButtonClickListenerBehavior implements Button.OnClickListener {
		
		int id;
		Boolean isOn = false;
		
		public ButtonClickListenerBehavior(int id){
			this.id = id;
			
		}
		
		@Override
		public void onClick(View arg0) {
			if(!isOn){
			try {
				// send a string to the Arduino Server in the form of
				// "set: -seekbarvalue- \n"
				dataOutputStream.writeBytes("p" + (char)id + " : "+ (char)1 + '\n');
			} catch (UnknownHostException e) {// catch and
				outputText(e.getMessage());// display errors
			} catch (IOException e) {// catch and
				outputText(e.getMessage());// display errors
			}
			isOn=true;
			}else{
				try {
					// send a string to the Arduino Server in the form of
					// "set: -seekbarvalue- \n"
					dataOutputStream.writeBytes("p" + (char)id + " : "+ (char)0 + '\n');
				} catch (UnknownHostException e) {// catch and
					outputText(e.getMessage());// display errors
				} catch (IOException e) {// catch and
					outputText(e.getMessage());// display errors
				}
				isOn = false;
				
			}
			
		}
		
	}



	// ----------------------- CONNECTION BUTTON EVENTLISTENER - begin
	// ----------------------------
	Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			if (!connected) {// if not connected yet
				outputText("connecting to Server");
				try {// try to create a socket and outputstream
					socket = new Socket("192.168.4.150", 6789);// create a
																// socket
					dataOutputStream = new DataOutputStream(
							socket.getOutputStream());// and stream
					outputText("successfully connected");// output the
															// connection status
					changeConnectionStatus(true);// change the connection status
				} catch (UnknownHostException e) {// catch and
					outputText(e.getMessage());// display errors
					changeConnectionStatus(false);
				} catch (IOException e) {// catch and
					outputText(e.getMessage());// display errors
					changeConnectionStatus(false);
				}
			} else {
				outputText("disconnecting from Server...");
				try {// try to close the socket
					socket.close();
					outputText("successfully disconnected");
					changeConnectionStatus(false);// change the connection
													// status
				} catch (UnknownHostException e) {// catch and
					outputText(e.getMessage());// display errors
				} catch (IOException e) {// catch and
					outputText(e.getMessage());// display errors
				}
			}
		}
		
		
		
		
		
		
	};

	// ----------------------- CONNECTION BUTTON EVENTLISTENER - END
	// ----------------------------


	/**
	 * listen for change of each bar, send out data to server , and change the text on screen
	 *  */
	/*
	public class SeekBarListenerBehavior implements SeekBar.OnSeekBarChangeListener{
		//bar id
		int id;    
		public  SeekBarListenerBehavior(int num){
			this.id = num;
		}
		@Override		
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			String valueOfseekbar = String.valueOf(progress);// save the value
																// of the
																// seekbar in a
																// string
			
			seekBarValue[id].setText(valueOfseekbar);// update the value in the
													// textfield

			if (connected) {// if the socket is connected
				try {
					// send a string to the Arduino Server in the form of
					// "set: -seekbarvalue- \n"
					dataOutputStream.writeBytes("p" + id + " : "+ valueOfseekbar + '\n');
				} catch (UnknownHostException e) {// catch and
					outputText(e.getMessage());// display errors
				} catch (IOException e) {// catch and
					outputText(e.getMessage());// display errors
				}
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
	}
	*/

}